package com.simba.server.components.data;

import java.sql.Timestamp;
import java.util.concurrent.atomic.AtomicBoolean;

import com.simba.server.commands.impl.fetch.FetchPacketByBoxCodeCmd;
import com.simba.server.commands.impl.record.RecordBoxPackCmd;
import com.simba.server.commands.impl.update.RenewBoxPackCmd;
import com.simba.server.commands.impl.web.WebPackSlotCmd;
import com.simba.server.components.CommandExecutor;
import org.apache.log4j.Logger;



import com.simba.common.log.LogUtil;

import com.simba.server.components.SlotManager;
import com.simba.server.components.SortProtocolCreator;

public class SlotInfo implements IRecordData
{
	private static final Logger logger = Logger.getLogger(SlotInfo.class);
	private Boolean allocBoxInfo = false;
	private final BoxInfo boxInfo = new BoxInfo();
	private int lockStatus;
	private int slotDistrict;
	private String slotId;
	private String slotType;
	private int sorterId;
	private String arriveCode="未知";
	private String arriveName="未知";
	private String slotName="未知";
	private int currentMailCount;
	private int totalMailCount;

	private int slotWhetherUpload;

	private String masterPrinterId;
	private String masterPrinterIp;
	private String slavePrinterId;
	private String slavePrinterIp;

	private final AtomicBoolean isPacking = new AtomicBoolean(false);

	public void cloneSlotInfo(SlotInfo toSlotInfo)
	{
		this.slotDistrict = toSlotInfo.getSlotDistrict();
		this.slotId = toSlotInfo.getSlotId();
		this.sorterId = toSlotInfo.getSorterId();

		this.masterPrinterId = toSlotInfo.getMasterPrinterId();
		this.masterPrinterIp = toSlotInfo.getMasterPrinterIp();
		this.slavePrinterId = toSlotInfo.getSlavePrinterId();
		this.slavePrinterIp = toSlotInfo.getSlavePrinterIp();

		this.boxInfo.setBoxCode(toSlotInfo.getBoxInfo().getBoxCode());
		this.boxInfo.setBoxType(toSlotInfo.getBoxInfo().getBoxType());
		this.boxInfo.setMails(toSlotInfo.getBoxInfo().getMails());
		this.boxInfo.setUploadFlag(0);
		this.boxInfo.setUploadNum(0);
	}

	public String getArriveCode() {
		return arriveCode;
	}

	public void setArriveCode(String arriveCode) {
		this.arriveCode = arriveCode;
	}

	public String getArriveName() {
		return arriveName;
	}

	public void setArriveName(String arriveName) {
		this.arriveName = arriveName;
	}

	public String getSlotName() {
		return slotName;
	}

	public void setSlotName(String slotName) {
		this.slotName = slotName;
	}

	public int getCurrentMailCount() {
		return currentMailCount;
	}

	public void setCurrentMailCount(int currentMailCount) {
		this.currentMailCount = currentMailCount;
		SlotManager.getInstance().sendSlotInfoToScreen(this.slotId);
	}

	public int getTotalMailCount() {
		return totalMailCount;
	}

	public void setTotalMailCount(int totalMailCount) {
		this.totalMailCount = totalMailCount;
	}

	public AtomicBoolean getIsPacking() {
		return isPacking;
	}

	public Boolean getAllocBoxInfo()
	{
		return this.allocBoxInfo;
	}

	public BoxInfo getBoxInfo()
	{
		return this.boxInfo;
	}

	public int getLockStatus()
	{
		return this.lockStatus;
	}

	@Override
	public String getRecordDataName()
	{
		return "Slot-Pack-Restore-Info";
	}


	public int getSlotDistrict()
	{
		return this.slotDistrict;
	}

	public String getSlotId()
	{
		return this.slotId;
	}

	public String getSlotType()
	{
		return slotType;
	}

	public int getSlotWhetherUpload()
	{
		return slotWhetherUpload;
	}

	public void resetBoxInfo()
	{
		this.allocBoxInfo = false;
		this.boxInfo.setId(0);
		this.boxInfo.setBoxCode("");
		this.boxInfo.setBoxType("");
		this.boxInfo.setMails(null);
		this.boxInfo.setUploadFlag(0);
		this.boxInfo.setUploadNum(0);

		this.setCurrentMailCount(0);
	}

	@Override
	public void restoreData()
	{
		Boolean res = false;
		try
		{
			LogUtil.LogDebug(SlotInfo.logger,
					"[SlotInfo.restoreData] Try Restore Slot Pack Again. [BoxCode:"
							+ this.boxInfo.getBoxCode() + "]");

			/**
			 * 收容口不打印包牌
			 */
			res = SlotManager.getInstance().checkSlotIsTrash(this.getSlotId());
			if (res)
			{
				this.setCurrentMailCount(0);
				return;
			}

			/**
			 * 指定格口不打包上传
			 */
			if (this.getSlotWhetherUpload() != 0)
			{
				LogUtil.LogDebug(SlotInfo.logger,
						"This Slot Shouldn't Pack : [" + this.getSlotId() + "]");

				this.resetBoxInfo();
				return;
			}

			String boxCode = this.getBoxInfo().getBoxCode();
			if (boxCode == null || boxCode.equals(""))
			{
				LogUtil.LogDebug(SlotInfo.logger,
						"Slot Box Code Is Null : [" + this.getSlotId() + "]");
				this.setCurrentMailCount(0);
				return;
			}

			res = CommandExecutor.executorSqlCommand(FetchPacketByBoxCodeCmd.NAME,  this.getBoxInfo(),false);
			if (!res)
			{
				LogUtil.LogDebug(SlotInfo.logger, "Query Packet Off Count In SlotInfo Failed ");
				return;
			}

			/**
			 * 0落格不打印包牌
			 */
			if (this.getBoxInfo().getMails().size() == 0)
			{
				LogUtil.LogDebug(SlotInfo.logger, "Query Packet Off Count is Zero");
				this.setCurrentMailCount(0);
				return;
			}

			/*BoxInfo boxInfo = this.getBoxInfo();
			boxInfo.setPlanId(boxInfo.getMails().get(0).getPlanId());
			boxInfo.setRoadSlot(boxInfo.getMails().get(0).getRoadSlot());*/
			this.getBoxInfo().setRoadSlot(this.getArriveName());

			Timestamp createTime = SortProtocolCreator.getInstance().createCurrentTimeStamp();
			this.getBoxInfo().setCreateTime(createTime.toString());

			res = CommandExecutor.executorSqlCommand(RecordBoxPackCmd.NAME,  this, false);
			if (!res)
			{
				LogUtil.LogDebug(SlotInfo.logger, "Insert Slot Pack Record In SlotInfo Failed ");
				return;
			}

            int uploadNum = this.getBoxInfo().getUploadNum() + 1;
			this.getBoxInfo().setUploadNum(uploadNum);
			this.getBoxInfo().setUploadFlag(1);
//			this.getBoxInfo().setBoxType("success");

			res = CommandExecutor.executorWebCommand(WebPackSlotCmd.NAME,  this.getBoxInfo(),false);

			if (!res)
			{
				LogUtil.LogDebug(SlotInfo.logger, "Upload Slot Pack To Remote Failed ");
				this.getBoxInfo().setUploadFlag(0);
//				this.getBoxInfo().setBoxType("remote failed");
			}

			CommandExecutor.executorSqlCommand(RenewBoxPackCmd.NAME,  this.getBoxInfo(),false);

			this.resetBoxInfo();
		}
		catch (Exception ex)
		{
			LogUtil.LogError(SlotInfo.logger,
					"Handle Slot Pack Message In SlotInfo Error -- Exception : " + ex);
		}

	}

	public void setAllocBoxInfo(Boolean status)
	{
		this.allocBoxInfo = status;
	}

	public void setLockStatus(int lockStatus)
	{
		this.lockStatus = lockStatus;
	}

	public void setSlotDistrict(int slotDistrict)
	{
		this.slotDistrict = slotDistrict;
	}

	public void setSlotId(String slotId)
	{
		this.slotId = slotId;
	}

	public void setSlotType(String slotType)
	{
		this.slotType = slotType;
	}

	public void setSlotWhetherUpload(int slotWhetherUpload)
	{
		this.slotWhetherUpload = slotWhetherUpload;
	}

	public int getSorterId() {
		return sorterId;
	}

	public void setSorterId(int sorterId) {
		this.sorterId = sorterId;
	}

	public String getMasterPrinterId() {
		return masterPrinterId;
	}

	public void setMasterPrinterId(String masterPrinterId) {
		this.masterPrinterId = masterPrinterId;
	}

	public String getMasterPrinterIp() {
		return masterPrinterIp;
	}

	public void setMasterPrinterIp(String masterPrinterIp) {
		this.masterPrinterIp = masterPrinterIp;
	}

	public String getSlavePrinterId() {
		return slavePrinterId;
	}

	public void setSlavePrinterId(String slavePrinterId) {
		this.slavePrinterId = slavePrinterId;
	}

	public String getSlavePrinterIp() {
		return slavePrinterIp;
	}

	public void setSlavePrinterIp(String slavePrinterIp) {
		this.slavePrinterIp = slavePrinterIp;
	}
}
